package com.vimi8.ebb.api.datareport;

import com.vimi8.ebb.configuration.HttpResponseEntity;
import com.vimi8.ebb.model.GeoCodings;
import com.vimi8.ebb.param.PlatformStatisticsParam;
import com.vimi8.ebb.service.ElectricityBusinessService;
import com.vimi8.ebb.util.GeoCodingUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;

/**
 * 电商平台API
 * Created by xiehuang on 2017/4/10.
 */
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringCodegen", date = "2016-09-19T10:01:46.405Z")

@Controller
public class ElectricityBusinessFrontApiController implements ElectricityBusinessFrontApi {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger("ElectricityBusinessFrontApiController");
    @Autowired
    private ElectricityBusinessService electricityBusinessService;

    @Override
    public HttpResponseEntity<List<HashMap<String, Object>>> electricityBusinessStatistics(
            @ApiParam(value = "地址编码", required = false) @RequestParam(value = "addressCode", required = false) String addressCode,
            @ApiParam(value = "年-月（如：2017-01）", required = false) @RequestParam(value = "yearAndMonth", required = false) String yearAndMonth,
            @ApiParam(value = "年份", required = false) @RequestParam(value = "year", required = false) String year,
            @ApiParam(value = "季度", required = false) @RequestParam(value = "quarter", required = false) String quarter
    ) {
        if (!StringUtils.isEmpty(addressCode)) {
            GeoCodings geoCodings = GeoCodingUtil.convertById(addressCode);//将地址编码转换为GeoCodings
            addressCode = GeoCodingUtil.separateAddressCode(addressCode, geoCodings.getRegionLever());//根据地址编码和城市级别将地址编码转换为含通配符的字符串
            logger.info("address_detail="+addressCode);
            logger.info("geoCodings.getRegionLever()="+geoCodings.getRegionLever());
        }
        List<HashMap<String, Object>> result = electricityBusinessService.getElectricityBusinessStatistics(addressCode, yearAndMonth, year, quarter);//查询电商平台产品交易量的统计信息
        return new HttpResponseEntity<List<HashMap<String, Object>>>(result, HttpStatus.OK);
    }

    @Override
    public HttpResponseEntity<HashMap<String, Object>> electricityThreePlatformStatistics(@ApiParam(value = "地址编码", required = false) @RequestParam(value = "addressCode", required = false) String addressCode, @ApiParam(value = "年-月（如：2017-01）", required = false) @RequestParam(value = "yearAndMonth", required = false) String yearAndMonth, @ApiParam(value = "年份", required = false) @RequestParam(value = "year", required = false) String year, @ApiParam(value = "季度", required = false) @RequestParam(value = "quarter", required = false) String quarter) {
        if (!StringUtils.isEmpty(addressCode)) {
            GeoCodings geoCodings = GeoCodingUtil.convertById(addressCode);//将地址编码转换为GeoCodings
            addressCode = GeoCodingUtil.separateAddressCode(addressCode, geoCodings.getRegionLever());//根据地址编码和城市级别将地址编码转换为含通配符的字符串
        }
        HashMap<String, Object> result = electricityBusinessService.getThreePlatformStatistics(addressCode, yearAndMonth, year, quarter);//查询电商平台产品交易量的统计信息
        return new HttpResponseEntity<HashMap<String, Object>>(result, HttpStatus.OK);
    }

    @Override
    public HttpResponseEntity<List<HashMap<String, Object>>> platformStatistics(PlatformStatisticsParam param) {
        if (!StringUtils.isEmpty(param.getAddressCode())) {
            GeoCodings geoCodings = GeoCodingUtil.convertById(param.getAddressCode());//将地址编码转换为GeoCodings
            param.setAddressCode( GeoCodingUtil.separateAddressCode(param.getAddressCode(), geoCodings.getRegionLever()));//根据地址编码和城市级别将地址编码转换为含通配符的字符串
            logger.info("address_detail="+param.getAddressCode());
            logger.info("geoCodings.getRegionLever()="+geoCodings.getRegionLever());
        }
        List<HashMap<String, Object>> result = electricityBusinessService.getPlatformStatistics(param);//查询电商平台的统计信息
        return new HttpResponseEntity<List<HashMap<String, Object>>>(result, HttpStatus.OK);
    }


}
